package com.bysj.lms.controller;


import com.bysj.lms.common.ResultModel;
import com.bysj.lms.pojo.dto.LmsBrDto;
import com.bysj.lms.pojo.entity.LmsBr;
import com.bysj.lms.properties.UploadProperties;
import com.bysj.lms.service.ILmsBrService;
import com.bysj.lms.utils.UploadUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaojiakai
 * @since 2022-02-04
 */
@Api(tags = "借还管理")
@RestController
@RequestMapping("/lms-br")
public class LmsBrController {


    @Autowired
    ILmsBrService lmsBrService;

    @ApiOperation("借还模型")
    @GetMapping("/getModel")
    public LmsBr getModel(){
        return new LmsBr();
    }

    @ApiOperation(value = "申请借出")
    @PutMapping("/add")
    public ResultModel add(@RequestBody LmsBr lmsBr){

        lmsBr.setStatus("0");
        System.out.println(lmsBr.toString());
        boolean save = lmsBrService.save(lmsBr);
        System.out.println(lmsBr.getCreateTime());
        if(save) return ResultModel.success("申请成功");
        return ResultModel.failure("申请失败");
    }
    @ApiOperation(value = "图片上传",notes = "返回imgUrl")
    @PostMapping("/upload")
    public ResultModel upload(@RequestParam("img") MultipartFile img){
        String path = UploadUtil.getPath();
        String imgUrl = UploadUtil.upload(img,path);
        return ResultModel.success("上传成功","/image/"+imgUrl);
    }

    @ApiOperation(value = "批准处理",notes = "传id，instrumentId，type，status")
    @PutMapping("/approve")
    public ResultModel approve(@RequestBody LmsBr lmsBr){
        boolean approve = lmsBrService.approve(lmsBr);
        if(approve) return ResultModel.success("操作成功");
        return ResultModel.failure("操作失败");
    }

    @ApiOperation(value = "批量处理",notes = "批量同意或批量拒绝，传对象数组，对象包括id，instrumentId，type")
    @PutMapping("/approveBatch")
    public ResultModel approveBatch(@RequestBody List<LmsBr> lmsBrs){
        boolean b = lmsBrService.approveBatch(lmsBrs);
        if(b) return ResultModel.success("操作成功");
        return ResultModel.failure("操作失败");
    }

    @ApiOperation(value = "归还")
    @PutMapping("/return")
    public ResultModel giveBack(String id){
        boolean giveBack = lmsBrService.giveBack(id);
        if(giveBack) return ResultModel.success("归还成功");
        return ResultModel.failure("归还失败");
    }

    @ApiOperation(value = "查看借还信息")
    @GetMapping("/find")
    public ResultModel find(String name,String uid,String status,Integer page,Integer count){
        PageInfo<LmsBrDto> pageInfo = lmsBrService.findByPage(name,uid,status, page, count);
        System.out.println(pageInfo.getList());
        return ResultModel.success("查找成功",pageInfo);
    }
}

